from sanic import Sanic
import redis
from sanic import response
import Capcha
from io import BytesIO

app = Sanic()
r = redis.StrictRedis(host="127.0.0.1", port=6379)


@app.route("/")
async def test(request):
    return response.text('Hello World!')


@app.route("/capcha")
async def getchacha(request):
    print(request.raw_args)
    f = BytesIO()
    code, image = Capcha.veri_code(4, 180, 80, 48)
    r.set("capcha", code)
    image.save(f, "jpeg")
    return response.raw(f.getbuffer(), 200, None, "image/jpeg")


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)
